﻿import flash.external.*

res_name_lst = []			//资源文件列表
bool_lst = ["是", "否"]		//是否列表
layer_sel_lst = []			//层次列表
lay_index = 0				//层次选择的编号，默认为0
layer_visible_lst = [true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true]
enable_chars = "" //允许输入的字符

//初始化一些属性
function init()
{
	library_panel.layer_selector.dataProvider = layer_sel_lst
	library_panel.layer_visible_selector.dataProvider = bool_lst
	i = 0;
	for(i = 0; i < 26; ++i)
	{
		enable_chars += String.fromCharCode(65+ i);
		enable_chars += String.fromCharCode(97 + i);
	}
	for(i = 0; i < 10; ++i)
	{
		enable_chars += String.fromCharCode(48 + i);
	}
	enable_chars += '_'
	d3_panel.name_txt.ableInputChars = enable_chars
	d3_panel.cls_txt.ableInputChars = enable_chars
	d3_panel.module_txt.ableInputChars = enable_chars
	d2_panel.name_txt.ableInputChars = enable_chars
	d2_panel.cls_txt.ableInputChars = enable_chars
	d2_panel.module_txt.ableInputChars = enable_chars
}

function reload_res_lst_panel_data()
{
	library_panel.res_lst_panel.dataProvider = res_name_lst	
}

//切换空对象 按钮事件
function empty_obj_btn_click()
{
	ExternalInterface.call("myeditor_editor.change_empty_type");
}

library_panel.empty_obj_btn.addEventListener("click", this, "empty_obj_btn_click")


//image2d对象 按钮事件
function image2d_btn_click()
{
	ExternalInterface.call("myeditor_editor.change_image2d_type");
}

library_panel.image2d_btn.addEventListener("click", this, "image2d_btn_click")

//model2d对象 按钮事件
function model2d_btn_click()
{
	ExternalInterface.call("myeditor_editor.change_model2d_type");
}

library_panel.model2d_btn.addEventListener("click", this, "model2d_btn_click")

//sprite2d对象 按钮事件
function sprite2d_btn_click()
{
	ExternalInterface.call("myeditor_editor.change_sprite2d_type");
}

library_panel.sprite2d_btn.addEventListener("click", this, "sprite2d_btn_click")

//model3d对象 按钮事件
function model3d_btn_click()
{
	ExternalInterface.call("myeditor_editor.change_model3d_type");
}

library_panel.model3d_btn.addEventListener("click", this, "model3d_btn_click")

//资源栏中选中某项
function res_sel_item_click(e:Object)
{
	ExternalInterface.call("myeditor_editor.sel_one_res", e.index);
}

library_panel.res_lst_panel.addEventListener("itemClick", this, "res_sel_item_click")

//创建游戏对象
function create_btn_click()
{
	ExternalInterface.call("myeditor_editor.create_gameobj");
}
library_panel.create_btn.addEventListener("click", this, "create_btn_click")


//格子宽度变化
function grid_w_change()
{
	unit_w = Number(library_panel.grid_w_txt.text)
	ExternalInterface.call("myeditor_editor.set_grid_w", unit_w);
}
library_panel.grid_w_txt.addEventListener("focusOut", this, "grid_w_change")
library_panel.grid_w_txt.addEventListener("submit", this, "grid_w_change")

//格子高度变化
function grid_h_change()
{
	unit_h =  Number(library_panel.grid_h_txt.text)
	ExternalInterface.call("myeditor_editor.set_grid_h", unit_h);
}
library_panel.grid_h_txt.addEventListener("focusOut", this, "grid_h_change")
library_panel.grid_h_txt.addEventListener("submit", this, "grid_h_change")

function enable_keyboard()
{
	ExternalInterface.call("myeditor_editor.enable_keyboard", true)
}

function disable_keyboard()
{
	ExternalInterface.call("myeditor_editor.enable_keyboard", false)
}
library_panel.grid_w_txt.addEventListener("focusIn", this, "enable_keyboard")
library_panel.grid_h_txt.addEventListener("focusIn", this, "enable_keyboard")
library_panel.grid_w_txt.addEventListener("focusOut", this, "disable_keyboard")
library_panel.grid_h_txt.addEventListener("focusOut", this, "disable_keyboard")


//层次选择改变
function layer_sel_change(e:Object)
{
	lay_index = e.index
	if(layer_visible_lst[lay_index])
	{
		library_panel.layer_visible_selector.selectedIndex = 0
	}
	else
	{
		library_panel.layer_visible_selector.selectedIndex = 1
	}
	
	ExternalInterface.call("myeditor_editor.set_edit_layer", lay_index);
}
library_panel.layer_selector.addEventListener("change", this, "layer_sel_change")

function set_layer_visible(e:Object)
{
	if(e.index == 0)
	{
		ExternalInterface.call("myeditor_editor.show_layer", lay_index);
		layer_visible_lst[lay_index] = true
	}
	if(e.index == 1)
	{
		ExternalInterface.call("myeditor_editor.hide_layer", lay_index);
		layer_visible_lst[lay_index] = false
	}
}
library_panel.layer_visible_selector.addEventListener("change", this, "set_layer_visible")

//属性框输入发生变化
function prop_change(e:Object)
{
	attr_name = e.target.defaultText
	attr_value = e.target.text
	arg = attr_name + "," + attr_value
	ExternalInterface.call("myeditor_editor.set_sel_obj_attr", arg)
}

//刷新属性框
function refresh_props()
{
	ExternalInterface.call("myeditor_editor.refresh_props")
}

//3d属性面板关联
d3_panel.alpha_txt.addEventListener("focusOut", this, "prop_change")
d3_panel.name_txt.addEventListener("focusOut", this, "prop_change")
d3_panel.module_txt.addEventListener("focusOut", this, "prop_change")
d3_panel.cls_txt.addEventListener("focusOut", this, "prop_change")
d3_panel.pos_x_txt.addEventListener("focusOut", this, "prop_change")
d3_panel.pos_y_txt.addEventListener("focusOut", this, "prop_change")
d3_panel.pos_x_txt.addEventListener("focusOut", this, "refresh_props")
d3_panel.pos_y_txt.addEventListener("focusOut", this, "refresh_props")
d3_panel.kp_x_txt.addEventListener("focusOut", this, "prop_change")
d3_panel.kp_y_txt.addEventListener("focusOut", this, "prop_change")
d3_panel.scale_x_txt.addEventListener("focusOut", this, "prop_change")
d3_panel.scale_y_txt.addEventListener("focusOut", this, "prop_change")
d3_panel.scale_z_txt.addEventListener("focusOut", this, "prop_change")
d3_panel.d3_x_txt.addEventListener("focusOut", this, "prop_change")
d3_panel.d3_y_txt.addEventListener("focusOut", this, "prop_change")
d3_panel.d3_z_txt.addEventListener("focusOut", this, "prop_change")
d3_panel.d3_x_txt.addEventListener("focusOut", this, "refresh_props")
d3_panel.d3_y_txt.addEventListener("focusOut", this, "refresh_props")
d3_panel.d3_z_txt.addEventListener("focusOut", this, "refresh_props")
d3_panel.rot_x_txt.addEventListener("focusOut", this, "prop_change")
d3_panel.rot_y_txt.addEventListener("focusOut", this, "prop_change")
d3_panel.rot_z_txt.addEventListener("focusOut", this, "prop_change")
//2d面板关联
d2_panel.alpha_txt.addEventListener("focusOut", this, "prop_change")
d2_panel.depth_txt.addEventListener("focusOut", this, "prop_change")
d2_panel.layer_id_txt.addEventListener("focusOut", this, "prop_change")
d2_panel.name_txt.addEventListener("focusOut", this, "prop_change")
d2_panel.module_txt.addEventListener("focusOut", this, "prop_change")
d2_panel.cls_txt.addEventListener("focusOut", this, "prop_change")
d2_panel.rot_txt.addEventListener("focusOut", this, "prop_change")
d2_panel.pos_x_txt.addEventListener("focusOut", this, "prop_change")
d2_panel.pos_y_txt.addEventListener("focusOut", this, "prop_change")
d2_panel.kp_x_txt.addEventListener("focusOut", this, "prop_change")
d2_panel.kp_y_txt.addEventListener("focusOut", this, "prop_change")
d2_panel.scale_x_txt.addEventListener("focusOut", this, "prop_change")
d2_panel.scale_y_txt.addEventListener("focusOut", this, "prop_change")
d2_panel.size_x_txt.addEventListener("focusOut", this, "prop_change")
d2_panel.size_y_txt.addEventListener("focusOut", this, "prop_change")

//3d属性面板关联
d3_panel.alpha_txt.addEventListener("submit", this, "prop_change")
d3_panel.name_txt.addEventListener("submit", this, "prop_change")
d3_panel.module_txt.addEventListener("submit", this, "prop_change")
d3_panel.cls_txt.addEventListener("submit", this, "prop_change")
d3_panel.pos_x_txt.addEventListener("submit", this, "prop_change")
d3_panel.pos_y_txt.addEventListener("submit", this, "prop_change")
d3_panel.pos_x_txt.addEventListener("submit", this, "refresh_props")
d3_panel.pos_y_txt.addEventListener("submit", this, "refresh_props")
d3_panel.kp_x_txt.addEventListener("submit", this, "prop_change")
d3_panel.kp_y_txt.addEventListener("submit", this, "prop_change")
d3_panel.scale_x_txt.addEventListener("submit", this, "prop_change")
d3_panel.scale_y_txt.addEventListener("submit", this, "prop_change")
d3_panel.scale_z_txt.addEventListener("submit", this, "prop_change")
d3_panel.d3_x_txt.addEventListener("submit", this, "prop_change")
d3_panel.d3_y_txt.addEventListener("submit", this, "prop_change")
d3_panel.d3_z_txt.addEventListener("submit", this, "prop_change")
d3_panel.d3_x_txt.addEventListener("submit", this, "refresh_props")
d3_panel.d3_y_txt.addEventListener("submit", this, "refresh_props")
d3_panel.d3_z_txt.addEventListener("submit", this, "refresh_props")
d3_panel.rot_x_txt.addEventListener("submit", this, "prop_change")
d3_panel.rot_y_txt.addEventListener("submit", this, "prop_change")
d3_panel.rot_z_txt.addEventListener("submit", this, "prop_change")
//2d面板关联
d2_panel.alpha_txt.addEventListener("submit", this, "prop_change")
d2_panel.depth_txt.addEventListener("submit", this, "prop_change")
d2_panel.layer_id_txt.addEventListener("submit", this, "prop_change")
d2_panel.name_txt.addEventListener("submit", this, "prop_change")
d2_panel.module_txt.addEventListener("submit", this, "prop_change")
d2_panel.cls_txt.addEventListener("submit", this, "prop_change")
d2_panel.rot_txt.addEventListener("submit", this, "prop_change")
d2_panel.pos_x_txt.addEventListener("submit", this, "prop_change")
d2_panel.pos_y_txt.addEventListener("submit", this, "prop_change")
d2_panel.kp_x_txt.addEventListener("submit", this, "prop_change")
d2_panel.kp_y_txt.addEventListener("submit", this, "prop_change")
d2_panel.scale_x_txt.addEventListener("submit", this, "prop_change")
d2_panel.scale_y_txt.addEventListener("submit", this, "prop_change")
d2_panel.size_x_txt.addEventListener("submit", this, "prop_change")
d2_panel.size_y_txt.addEventListener("submit", this, "prop_change")

//使所有的输入框失去焦点
function focusout_all_input()
{
	Selection.setFocus(null)
}

//菜单按钮
//载入场景文件
function load_sce_file()
{
	ExternalInterface.call("myeditor_editor.load_sce_file")
}
menu.load_btn.addEventListener("click", this, "load_sce_file")

function save_sce_file()
{
	ExternalInterface.call("myeditor_editor.save_sce_file")
}
menu.save_btn.addEventListener("click", this, "save_sce_file")

function sel_btn_click()
{
	ExternalInterface.call("myeditor_editor.set_mouse_tool", 0)
}
menu.mouse_sel_btn.addEventListener("click", this, "sel_btn_click")

function pen_btn_click()
{
	ExternalInterface.call("myeditor_editor.set_mouse_tool", 1)
}
menu.mouse_pen_btn.addEventListener("click", this, "pen_btn_click")

function tube_btn_click()
{
	ExternalInterface.call("myeditor_editor.set_mouse_tool", 2)
}
menu.mouse_tube_btn.addEventListener("click", this, "tube_btn_click")

function col_sphere_btn_click()
{
	ExternalInterface.call("myeditor_editor.set_mouse_tool", 3)
}
menu.col_sphere_btn.addEventListener("click", this, "col_sphere_btn_click")

function col_rect_btn_click()
{
	ExternalInterface.call("myeditor_editor.set_mouse_tool", 4)
}
menu.col_rect_btn.addEventListener("click", this, "col_rect_btn_click")

function col_poly_btn_click()
{
	ExternalInterface.call("myeditor_editor.set_mouse_tool", 5)
}
menu.col_poly_btn.addEventListener("click", this, "col_poly_btn_click")

function align_grid_btn_click()
{
	menu.align_grid_btn.selected = not menu.align_grid_btn.selected
	ExternalInterface.call("myeditor_editor.switch_grid_align")
}
menu.align_grid_btn.addEventListener("click", this, "align_grid_btn_click")

function lib_visible_btn_click()
{
	menu.lib_visible_btn.selected = not menu.lib_visible_btn.selected
	ExternalInterface.call("myeditor_editor.switch_lib_view")
}
menu.lib_visible_btn.addEventListener("click", this, "lib_visible_btn_click")

col_debug_flag = True

function col_debug_btn_click()
{
	menu.col_debug_btn.selected = not menu.col_debug_btn.selected
	ExternalInterface.call("myeditor_editor.enable_col_debug_view", col_debug_flag)
	col_debug_flag = not col_debug_flag
}
menu.col_debug_btn.addEventListener("click", this, "col_debug_btn_click")

function camera_reset_btn_click()
{
	ExternalInterface.call("myeditor_editor.camera_reset")
}
menu.camera_reset_btn.addEventListener("click", this, "camera_reset_btn_click")

function help_btn_click()
{
	ExternalInterface.call("myeditor_editor.show_help")
}
menu.info_btn.addEventListener("click", this, "help_btn_click")

function exit_btn_click()
{
	ExternalInterface.call("myeditor_editor.exit_editor")
}
menu.exit_btn.addEventListener("click", this, "exit_btn_click")

grid_visible = true			//格子的显示
function grid_visible_btn_click()
{
	if(not grid_visible)
	{
		ExternalInterface.call("myeditor_editor.show_grid")
	}
	else
	{
		ExternalInterface.call("myeditor_editor.hide_grid")
	}
	grid_visible = not grid_visible
	menu.grid_visible_btn.selected = not menu.grid_visible_btn.selected
}
menu.grid_visible_btn.addEventListener("click", this, "grid_visible_btn_click")

function del_btn_click()
{
	ExternalInterface.call("myeditor_editor.del_sel_obj")
}
library_panel.del_btn.addEventListener("click", this, "del_btn_click")

function show_tips(e:Object)
{
	ExternalInterface.call("myeditor_editor.show_tips", e.tips)
}
menu.load_btn.addEventListener("showTips", this, "show_tips")
menu.save_btn.addEventListener("showTips", this, "show_tips")
menu.mouse_sel_btn.addEventListener("showTips", this, "show_tips")
menu.mouse_pen_btn.addEventListener("showTips", this, "show_tips")
menu.mouse_tube_btn.addEventListener("showTips", this, "show_tips")
menu.col_sphere_btn.addEventListener("showTips", this, "show_tips")
menu.col_rect_btn.addEventListener("showTips", this, "show_tips")
menu.col_poly_btn.addEventListener("showTips", this, "show_tips")
menu.col_debug_btn.addEventListener("showTips", this, "show_tips")
menu.align_grid_btn.addEventListener("showTips", this, "show_tips")
menu.grid_visible_btn.addEventListener("showTips", this, "show_tips")
menu.lib_visible_btn.addEventListener("showTips", this, "show_tips")
menu.camera_reset_btn.addEventListener("showTips", this, "show_tips")
menu.exit_btn.addEventListener("showTips", this, "show_tips")
menu.info_btn.addEventListener("showTips", this, "show_tips")

function close_tips(e:Object)
{
	ExternalInterface.call("myeditor_editor.close_tips")
}
menu.load_btn.addEventListener("closeTips", this, "close_tips")
menu.save_btn.addEventListener("closeTips", this, "close_tips")
menu.mouse_sel_btn.addEventListener("closeTips", this, "close_tips")
menu.mouse_pen_btn.addEventListener("closeTips", this, "close_tips")
menu.mouse_tube_btn.addEventListener("closeTips", this, "close_tips")
menu.col_sphere_btn.addEventListener("closeTips", this, "close_tips")
menu.col_rect_btn.addEventListener("closeTips", this, "close_tips")
menu.col_poly_btn.addEventListener("closeTips", this, "close_tips")
menu.col_debug_btn.addEventListener("closeTips", this, "close_tips")
menu.align_grid_btn.addEventListener("closeTips", this, "close_tips")
menu.grid_visible_btn.addEventListener("closeTips", this, "close_tips")
menu.lib_visible_btn.addEventListener("closeTips", this, "close_tips")
menu.camera_reset_btn.addEventListener("closeTips", this, "close_tips")
menu.exit_btn.addEventListener("closeTips", this, "close_tips")
menu.info_btn.addEventListener("closeTips", this, "close_tips")

stop();






